package server;

import dbtools.DataSimpDto;
import dbtools.GlobalParam;
import io.netty.channel.ChannelHandlerContext;
import model.Client;
import model.ClientGroup;
import model.TkInfo;

import java.util.logging.Level;
import java.util.logging.Logger;


public class Service {
	private static final Logger logger = Logger.getLogger(Service.class.getName());
	public static Control control;
	public static ClientGroup clientgroup=new ClientGroup();
	/**
	 * 接受信息
	 * @param tkinfo
	 */
	static public void doMsgRecieved(TkInfo tkinfo, ChannelHandlerContext ctx){
		logger.log(Level.INFO,"接收客户端信息"+tkinfo.getName());
		if(tkinfo.getName().equals("LOGIN"))//客户端请求登录
			NoticeNewConnect(tkinfo,ctx);
		else if(tkinfo.getName().equals("REQUEST"))//客户端请求信息
			NoticeStatus(tkinfo,ctx);
	}
	/**
	 * 新客户端接入进来的操作
	 * @param tkinfo
	 * @param ctx
	 */
	private static void NoticeNewConnect(TkInfo tkinfo,ChannelHandlerContext ctx) {
		Client client=new Client();
		String taskno=tkinfo.getTkMap("taskno");//获取唯一标识
		client.setTaskno(taskno);
		client.setMachineChannel(ctx.channel());
		if(clientgroup.hasChannel(taskno))
		{
			TkInfo outInfo=new TkInfo();
			outInfo.setName("LOGINRESULT");
			outInfo.addTkMaps("login", "failed");//登录成功
			outInfo.addTkMaps("description", "不能重复登录");
			ctx.writeAndFlush(outInfo);
		}
		else
		{
			//查询数据库中该节点的信息
			String sql="select " +
					" a.pk_task,a.pk_node,a.fd_taskno,a.fd_pl,a.fd_pastdel,a.fd_autostart, " +
					" a.fd_upml,a.fd_upmlbak,a.fd_ftpaddr,a.fd_ftpusr,a.fd_ftppw,a.fd_ftpmul," +
					" a.fd_stat,b.fd_fwno,b.fd_name,b.fd_addr,b.fd_stat" +
					" from t_fw_task a left join t_fw_node b on a.pk_node=b.pk_node "
					+"  where a.fd_taskno='"+taskno+"'";
			DataSimpDto dto=new DataSimpDto(sql);
			if(dto.getTotRecord()>0)
			{
				TkInfo outInfo=new TkInfo();
				outInfo.setName("LOGINRESULT");
				outInfo.addTkMaps("login", "success");//登录成功
				outInfo.addTkMaps("description", "登录成功");
				outInfo=addBaseInfo(outInfo,dto,taskno);
				outInfo=addDataBaseInfo(outInfo);
				client.setStatus(tkinfo.getTkMap("status"));
				clientgroup.addChannel(client);//给客户端发送成功信息
				ctx.writeAndFlush(outInfo);
				control.NoticeRunStatus(client);
			}else
			{
				TkInfo outInfo=new TkInfo();
				outInfo.setName("LOGINRESULT");
				outInfo.addTkMaps("login", "failed");//登录成功
				outInfo.addTkMaps("description", "节点未定义！！！！！！！！！！！！！！");
				ctx.writeAndFlush(outInfo);
			}
		}
	}
	/***
	 * 客户端发送过来状态指令
	 * @param tkinfo
	 * @param ctx
	 */
	private static void NoticeStatus(TkInfo tkinfo,ChannelHandlerContext ctx)
	{
		String TaskNo=tkinfo.getTkMap("taskno");
		String status=tkinfo.getTkMap("status");
		Client client=clientgroup.getChannel(TaskNo);
		client.setStatus(status);
		control.NoticeRunStatus(client);//处理客户端的状态信息
	}
	public static TkInfo addBaseInfo(TkInfo outInfo,DataSimpDto dto,String taskno)
	{
		outInfo.addTkMaps("machinecode", dto.getDataValue("fd_fwno", 0));//服务节点编号
		outInfo.addTkMaps("machinename", dto.getDataValue("fd_name", 0));//服务名称
		outInfo.addTkMaps("taskno", taskno);//任务编号
		outInfo.addTkMaps("scanperiod", dto.getDataValue("fd_pl", 0));		//扫描频率
		outInfo.addTkMaps("deleteafter", dto.getDataValue("fd_pastdel", 0));		//过期天数

		System.out.println("fu:"+dto.getDataValue("fd_pastdel", 0)+"=="+outInfo.getTkMap("deleteafter"));
		outInfo.addTkMaps("autostart", dto.getDataValue("fd_autostart", 0));			//自动启动



		outInfo.addTkMaps("uploadurl", dto.getDataValue("fd_upml", 0).replace("********", "\\"));		//上传目录
		outInfo.addTkMaps("uploadbakurl", dto.getDataValue("fd_upmlbak", 0).replace("********", "\\"));		//上传目录

		return outInfo;
	}
	public static TkInfo addDataBaseInfo(TkInfo tk)
	{
		tk.addTkMaps("jdbc_driver", GlobalParam.driver);
		tk.addTkMaps("jdbc_username",GlobalParam.username);
		tk.addTkMaps("jdbc_password",GlobalParam.password);
		tk.addTkMaps("jdbc_url",GlobalParam.url);
		tk.addTkMaps("jdbc_urlx",GlobalParam.urlx);
		tk.addTkMaps("jdbc_maxActive",GlobalParam.maxactive);
		return tk;
	}
}
